import {
    Component,
    ViewChild,
    Injector,
    Output,
    EventEmitter,
} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {
    PipelineServiceProxy,
    DepartmentServiceProxy,
    PrintServiceProxy,
    NameValueDtoOfInt32,
    ComboboxItemDto,
    PrintCommonDto,
    TaskDto,
    InputSideTaskDto,
    PrintInput,
} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';

@Component({
    selector: 'printdzdModal',
    templateUrl: './printdzd-modal.component.html',
})
export class PrintdzdModalComponent extends AppComponentBase {
    @ViewChild('actionModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    active = false;
    saving = false;
    departments = new Array<NameValueDtoOfInt32>();
    oldprint = '';
    teamGroups: ComboboxItemDto[] = [];
    teamGroupName: ComboboxItemDto;
    task: TaskDto;
    sideProperty='';
    remark = '';
    departmentValue: number | undefined;
    inputSideTaskDto = new InputSideTaskDto();
    receptionTime = new Date();
    printInput = new PrintCommonDto();
    
    constructor(
        injector: Injector,
        private _printServiceProxy: PrintServiceProxy
    ) {
        super(injector);
    }

    show(task: TaskDto): void {
        this.task = task;
        this.active = true;
        this.primengTableHelper.showLoadingIndicator();
        this._printServiceProxy.getPrintDrow().subscribe((result) => {
            // this.teamGroupName = result.groupName;
            this.teamGroups = result;
            this._printServiceProxy.getUserPrint().subscribe((result1) => {
                if (result1 != null) {
                    this.oldprint = result1.value;
                }
                this.teamGroupName = result1;
            });
        });
        this.modal.show();
    }

    save(): void {
        this.primengTableHelper.showLoadingIndicator();
        if ( this.oldprint !== this.teamGroupName.value) {
            this._printServiceProxy.createOrUpdateUserPrint(this.teamGroupName.value).subscribe(() => { } )
        }

        this.inputSideTaskDto.id = this.task.id;
        this.inputSideTaskDto.receptionTime = moment(this.receptionTime) ;
        this.inputSideTaskDto.remark = this.task.remark;
        this.inputSideTaskDto.sideProperty = this.task.sideProperty;
        this._printServiceProxy.updateSideTask(this.inputSideTaskDto).subscribe(() => { })

        this.printInput.printInput = new PrintInput();
        this.printInput.printSet = this.teamGroupName.value;
        this.printInput.type = 'DingZhiDan';
        this.printInput.printInput.dingZhi = this.task.originalProducerId + '车间';
        this.printInput.printInput.zhiXing = this.task.producerId + '车间';
        this.printInput.printInput.dingZhiTime = this.task.creationTime;
        this.printInput.printInput.taskCode = this.task.taskCode;
        this.printInput.printInput.dingZhiXingZhi = this.task.sideProperty;
        this.printInput.printInput.songHuiTime = moment( this.receptionTime);
        this.printInput.printInput.name = this.task.drawingName;
        this.printInput.printInput.drawCode = this.task.drawingCode;
        this.printInput.printInput.remark = this.task.remark;
        this.printInput.printInput.num = this.task.totalQuantity;
        this._printServiceProxy.print(this.printInput).subscribe(() => {
            this.notify.success('打印定制单成功');
            this.primengTableHelper.hideLoadingIndicator();
        })
    }
    close(): void {
        this.saving = false;
        this.modal.hide();
    }
}
